{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 确定链表字符串是否为回文串\n",
    "\n",
    "![Asked by Microsoft](https://img.shields.io/badge/Asked%20by-Microsoft-important?style=for-the-badge&logo=microsoft)\n",
    "\n",
    "&emsp;&emsp;给定一个双向链表，确定它是否为回文串。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def isPalindrome(node: dict) -> bool:\n",
    "    tail = node\n",
    "    while 'next' in tail.keys():\n",
    "        tail = tail['next']\n",
    "    while not node and not tail and node != tail:\n",
    "        if (node['value'] != tail['value']):\n",
    "            return False\n",
    "        node = node['next']\n",
    "        tail = tail['prev']\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "node = {'value': 'a'}\n",
    "node['next'] = {'value': 'b'}\n",
    "node['next']['prev'] = node\n",
    "node['next']['next'] = {'value': 'b'}\n",
    "node['next']['next']['prev'] = node['next']\n",
    "node['next']['next']['next'] = {'value': 'a'}\n",
    "node['next']['next']['next']['prev'] = node['next']['next']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(isPalindrome(node))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
